package com.malou.client.ventas.cajainicial;

import java.util.Date;

import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.malou.client.MalouConstants;
import com.malou.client.util.MensajeData;

public class CajaIniciales {
	
	private static CajaInicialServiceAsync si = GWT.create(CajaInicialService.class);
	private static MalouConstants constants = GWT.create(MalouConstants.class);
	public static CajaInicialForm cif;	
	
	public static void checkSaldoInicial(Date fecha){
		
		si.hascajainicial(fecha, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {					
				MessageBox.alert(constants.sistemaMarcocruzado(), constants.errorConexion(), null);					
			}

			@Override
			public void onSuccess(Boolean data) {
				if (data) {					
					cif.addMessage();
				}else {
					cif.addFields();
				}						
			}
			
		});
	}
	
	public static void save(CajaInicial balance) {
		
		
		si.save(balance, new AsyncCallback<MensajeData>() {

			@Override
			public void onFailure(Throwable caught) {				
				MessageBox.alert(constants.sistemaMarcocruzado(), constants.errorConexion(), null);			
			}

			@Override
			public void onSuccess(MensajeData data) {
				if (data.guardado) {				
					cif.addMessage();					
				}
				 else {					 
					 String msg;
					 if(data.errorTipo == 1){
						 msg = constants.cajainicialYaIngresado();
						 cif.addMessage();
					 }else{
						 msg = constants.cajainicialNoGuardado();
						 cif.unmask();
					 }
					 MessageBox.alert(constants.sistemaMarcocruzado(), msg, null);
					 
				}						
			}
			
		});
	}
	
	
}
